define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            var main_id = Fast.api.query('ids');
            Table.api.init({
                extend: {
                    index_url: 'mental/testreport/index' + location.search,
                    add_url: 'mental/testreport/add'+ '?main_id='+main_id,
                    edit_url: 'mental/testreport/edit',
                    del_url: 'mental/testreport/del',
                    multi_url: 'mental/testreport/multi',
                    import_url: 'mental/testreport/import',
                    table: 'mental_testreport',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams:function(params){
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var main_id = Fast.api.query('ids');  
                  
                    if (main_id) {
                          filter.main_id = main_id;
                          op.main_id = '=';
                          params.filter = JSON.stringify(filter);
                          params.op = JSON.stringify(op);
                        //   funcUrlDel('ids');
                    }
                    return params;
                  
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'dimensionConfig', title: __('Dimensionconfig'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
             // 启动和暂停按钮
            $(document).on("click", ".btn-copy", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                Table.api.multi("changestatus", ids.join(","), table, this);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
